<?php

namespace app\material\model;
use app\BaseModel;
use app\shop\model\GoodsModel;
use think\Db;

//*------------------------------------------------------ */
//-- 高清大图
/*------------------------------------------------------ */
class MaterialListModel extends BaseModel
{
	protected $table = 'material_list';

	public  $pk = 'id';

    function getList($where,$uid = 0,$page = 1,$limit = 10)
    {
        $model = new \app\mainadmin\model\SettingsModel();
        $goodsModel = new GoodsModel();
        $orderModel = new MaterialOrderModel();
        $list = $this->where($where)->order('id desc')->page($page,$limit)->select()->toArray();
        foreach ($list as &$value){
            if($value['uid']){
                $value['user'] = Db::name('users')->field('nick_name as nickname,headimgurl as avatar')->where(['user_id' => $value['uid']])->find();
                if($value['user']['avatar'] == '.'){
                    $value['user']['avatar'] = '';
                }
            }else{
                $value['user']['nickname'] = $model->where('name','site_name')->value('data');
                $value['user']['avatar'] = $model->where('name','logo')->value('data');
            }
            if($value['images']){
                $value['images'] = explode(',',$value['images']);
            }else{
                $value['images'] =  [];
            }
            if($value['goods_id']){
                $value['goods'] = $goodsModel->field('goods_img,goods_name,shop_price')->where(['goods_id' => $value['goods_id']])->find();
            }else{
                $value['goods'] = [];
            }
            $value['collect_count']  = Db::name('material_collect')->where(['mid' => $value['id']])->count();
            $value['is_collect']  = Db::name('material_collect')->where(['uid' => $uid,'mid' => $value['id']])->count();
            $value['is_buy'] = $orderModel->where(['uid' => $uid,'paid' => 1,'mid' => $value['id']])->count();
            $value['buy_count'] = $orderModel->where(['paid' => 1,'mid' => $value['id']])->count();
        }
        return $list;
    }

    public function getOne($id,$uid = 0)
    {
        $goodsModel = new GoodsModel();
        $orderModel = new MaterialOrderModel();
        $info = $this->where('id',$id)->find()->toArray();
        $info['add_time'] = date('Y-m-d H:i',$info['add_time']);
        if($info['uid']){
            $info['user'] = Db::name('users')->field('nick_name as nickname,headimgurl as avatar')->where(['user_id' => $info['uid']])->find();
            if($info['user']['avatar'] == '.'){
                $info['user']['avatar'] = '';
            }
        }else{
            $model = new \app\mainadmin\model\SettingsModel();
            $info['user']['nickname'] = $model->where('name','site_name')->value('data');

            $info['user']['avatar'] = $model->where('name','logo')->value('data');
        }
        if($info['goods_id']){
            $info['goods'] = $goodsModel->field('goods_img,goods_name,shop_price,shop_price as sale_price,goods_thumb,goods_id')->where(['goods_id' => $info['goods_id']])->find();
        }else{
            $info['goods'] = [];
        }
        $info['images'] = $info['images'] ? explode(',',$info['images']) : [];
        $info['collect_count']  = Db::name('material_collect')->where(['mid' => $info['id']])->count();
        $info['is_collect']  = Db::name('material_collect')->where(['uid' => $uid,'mid' => $info['id']])->count();
        $info['is_buy'] = $orderModel->where(['uid' => $uid,'paid' => 1,'mid' => $info['id']])->count();
        return $info;
    }
}
